package com.qianyu.project.web.controller.mfy;

import com.qianyu.project.core.context.user.FlowUserProvider;
import com.qianyu.project.entity.DeviceCollect;
import com.qianyu.project.entity.DeviceInspect;
import com.qianyu.project.service.*;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

/**
 * @Author mfy
 * @desc
 */
@RestController
@RequestMapping("/deviceInspects")
public class DeviceInspectController {

    @Autowired
    private IDeviceInspectService deviceInspectService;
    @Autowired
    private FlowUserProvider flowUserProvider;

    @Autowired
    private IDeptService deptService;
    @Autowired
    private IAttachemenInfoService attachemenInfoService;
    @Autowired
    private IDeviceInspectListService deviceInspectListService;

    @PostMapping("")
    public Result save(@RequestBody Map<String,Object> params){
        System.out.println(params);
        DeviceInspect di = new DeviceInspect();
        Map<String,Object> mapDrp = (Map<String, Object>) params.get("DeviceInspect");
        di.setInspectName(mapDrp.get("inspectName")+"");
        di.setPId(Integer.valueOf(mapDrp.get("pId")+""));
        di.setInspectId(Integer.valueOf(mapDrp.get("inspectId")+""));
        di.setDid(Integer.valueOf(mapDrp.get("did")+""));
        di.setFillUser(flowUserProvider.getFlowUser().getId().intValue());
        di.setUpdateDate(LocalDate.now().toString());
        di.setComment(mapDrp.get("comment")+"");
        di.setApproveStatus(1+"");
        deviceInspectService.saveOrUpdate(di);
        deviceInspectService.Mysave(di,params);
        return ResultTool.success();
    }

    @GetMapping("/getdept")
    public Result getdept(){
        return ResultTool.success(deptService.list());
    }


    @PutMapping("")
    public Result update(@RequestBody Map<String,Object> params){
        DeviceInspect di = new DeviceInspect();
        Map<String,Object> mapDrp = (Map<String, Object>) params.get("DeviceInspect");
        di.setId(Integer.valueOf(mapDrp.get("id")+""));
        di.setInspectName(mapDrp.get("inspectName")+"");
        di.setPId(Integer.valueOf(mapDrp.get("pId")+""));
        di.setInspectId(Integer.valueOf(mapDrp.get("inspectId")+""));
        di.setDid(Integer.valueOf(mapDrp.get("did")+""));
        di.setFillUser(flowUserProvider.getFlowUser().getId().intValue());
        di.setUpdateDate(LocalDate.now().toString());
        di.setComment(mapDrp.get("comment")+"");
        di.setApproveStatus(1+"");
        deviceInspectService.saveOrUpdate(di);
        deviceInspectService.Myupdate(di,params);
        return ResultTool.success();
    }
    @GetMapping("")
    public Result list(@RequestParam Map<String,String> params){
        return ResultTool.success(deviceInspectService.Mylist(params));
    }
    @GetMapping("/listNopage")
    public Result listNopage(@RequestParam Map<String,String> params){
        return ResultTool.success(deviceInspectService.listNopage(params));
    }

    @GetMapping("/selectEnclosures/{attachIds}")
    public Result selectEnclosures(@PathVariable String attachIds){
        return ResultTool.success(attachemenInfoService.selectEnclosures5(attachIds));
    }

    @GetMapping("/selectMaterialInfos/{id}")
    public Result selectMaterialInfos(@PathVariable String id){
        return ResultTool.success(deviceInspectListService.selectMaterialInfos(id));
    }
}
